/*     */ package com.zimbra.cs.dav.client;
/*     */ 
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavContext.Depth;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.httpclient.HttpProxyUtil;
/*     */ import com.zimbra.cs.service.UserServlet.HttpInputStream;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import java.io.IOException;
/*     */ import java.net.URL;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import org.apache.commons.httpclient.Cookie;
/*     */ import org.apache.commons.httpclient.Credentials;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.HttpState;
/*     */ import org.apache.commons.httpclient.UsernamePasswordCredentials;
/*     */ import org.apache.commons.httpclient.auth.AuthScope;
/*     */ import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.PutMethod;
/*     */ import org.apache.commons.httpclient.params.HttpClientParams;
/*     */ import org.apache.commons.httpclient.params.HttpMethodParams;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentException;
/*     */ import org.dom4j.QName;
/*     */ import org.dom4j.io.SAXReader;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class WebDavClient
/*     */ {
/*     */   private String mUserAgent;
/*     */   private String mBaseUrl;
/*     */   private String mUsername;
/*     */   private String mPassword;
/*     */   private HttpClient mClient;
/*     */   
/*     */   public WebDavClient(String baseUrl)
/*     */   {
/*  60 */     this(baseUrl, "ZCS");
/*     */   }
/*     */   
/*     */   public WebDavClient(String baseUrl, String app) {
/*  64 */     this.mBaseUrl = baseUrl;
/*  65 */     this.mClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  66 */     HttpProxyUtil.configureProxy(this.mClient);
/*  67 */     setAppName(app);
/*     */   }
/*     */   
/*     */   public Collection<DavObject> listObjects(String path, Collection<QName> extraProps) throws IOException, DavException {
/*  71 */     DavRequest propfind = DavRequest.PROPFIND(path);
/*  72 */     propfind.setDepth(DavContext.Depth.one);
/*  73 */     if (extraProps == null) {
/*  74 */       propfind.addRequestProp(DavElements.E_DISPLAYNAME);
/*  75 */       propfind.addRequestProp(DavElements.E_RESOURCETYPE);
/*  76 */       propfind.addRequestProp(DavElements.E_CREATIONDATE);
/*  77 */       propfind.addRequestProp(DavElements.E_GETCONTENTLENGTH);
/*  78 */       propfind.addRequestProp(DavElements.E_GETCONTENTLANGUAGE);
/*  79 */       propfind.addRequestProp(DavElements.E_GETCONTENTTYPE);
/*  80 */       propfind.addRequestProp(DavElements.E_GETETAG);
/*  81 */       propfind.addRequestProp(DavElements.E_GETLASTMODIFIED);
/*     */     } else {
/*  83 */       for (QName p : extraProps)
/*  84 */         propfind.addRequestProp(p);
/*     */     }
/*  86 */     return sendMultiResponseRequest(propfind);
/*     */   }
/*     */   
/*     */   public Collection<DavObject> sendMultiResponseRequest(DavRequest req) throws IOException, DavException {
/*  90 */     ArrayList<DavObject> ret = new ArrayList();
/*     */     
/*  92 */     HttpMethod m = null;
/*     */     try {
/*  94 */       m = executeFollowRedirect(req);
/*  95 */       int status = m.getStatusCode();
/*  96 */       if (status >= 400) {
/*  97 */         throw new DavException("DAV server returned an error: " + status, status);
/*     */       }
/*  99 */       Document doc = com.zimbra.common.soap.Element.getSAXReader().read(m.getResponseBodyAsStream());
/* 100 */       org.dom4j.Element top = doc.getRootElement();
/* 101 */       for (Object obj : top.elements(DavElements.E_RESPONSE)) {
/* 102 */         if ((obj instanceof org.dom4j.Element)) {
/* 103 */           ret.add(new DavObject((org.dom4j.Element)obj));
/*     */         }
/*     */       }
/*     */     } catch (DocumentException e) {
/* 107 */       throw new DavException("can't parse response", e);
/*     */     } finally {
/* 109 */       if (m != null)
/* 110 */         m.releaseConnection();
/*     */     }
/* 112 */     return ret;
/*     */   }
/*     */   
/*     */   public UserServlet.HttpInputStream sendRequest(DavRequest req) throws IOException, DavException {
/* 116 */     HttpMethod m = executeFollowRedirect(req);
/* 117 */     return new UserServlet.HttpInputStream(m);
/*     */   }
/*     */   
/*     */   public UserServlet.HttpInputStream sendGet(String href) throws IOException {
/* 121 */     GetMethod get = new GetMethod(this.mBaseUrl + href);
/* 122 */     executeMethod(get, DavContext.Depth.zero);
/* 123 */     return new UserServlet.HttpInputStream(get);
/*     */   }
/*     */   
/*     */   public UserServlet.HttpInputStream sendPut(String href, byte[] buf, String contentType, String etag, Collection<Pair<String, String>> headers) throws IOException {
/* 127 */     boolean done = false;
/* 128 */     PutMethod put = null;
/* 129 */     while (!done) {
/* 130 */       put = new PutMethod(this.mBaseUrl + href);
/* 131 */       put.setRequestEntity(new ByteArrayRequestEntity(buf, contentType));
/* 132 */       if ((this.mDebugEnabled) && (contentType.startsWith("text")))
/* 133 */         ZimbraLog.dav.debug("PUT payload: \n" + new String(buf, "UTF-8"));
/* 134 */       if (etag != null)
/* 135 */         put.setRequestHeader("If-Match", etag);
/* 136 */       if (headers != null)
/* 137 */         for (Pair<String, String> h : headers)
/* 138 */           put.addRequestHeader((String)h.getFirst(), (String)h.getSecond());
/* 139 */       executeMethod(put, DavContext.Depth.zero);
/* 140 */       int ret = put.getStatusCode();
/* 141 */       if ((ret == 301) || (ret == 302)) {
/* 142 */         Header newLocation = put.getResponseHeader("Location");
/* 143 */         if (newLocation != null) {
/* 144 */           href = newLocation.getValue();
/* 145 */           ZimbraLog.dav.debug("redirect to new url = " + href);
/* 146 */           put.releaseConnection();
/* 147 */           continue;
/*     */         }
/*     */       }
/* 150 */       done = true;
/*     */     }
/* 152 */     return new UserServlet.HttpInputStream(put);
/*     */   }
/*     */   
/*     */   protected HttpMethod executeFollowRedirect(DavRequest req) throws IOException {
/* 156 */     HttpMethod method = null;
/* 157 */     boolean done = false;
/* 158 */     while (!done) {
/* 159 */       method = execute(req);
/* 160 */       int ret = method.getStatusCode();
/* 161 */       if ((ret == 301) || (ret == 302)) {
/* 162 */         Header newLocation = method.getResponseHeader("Location");
/* 163 */         if (newLocation != null) {
/* 164 */           String uri = newLocation.getValue();
/* 165 */           ZimbraLog.dav.debug("redirect to new url = " + uri);
/* 166 */           method.releaseConnection();
/* 167 */           req.setRedirectUrl(uri);
/* 168 */           continue;
/*     */         }
/*     */       }
/* 171 */       done = true;
/*     */     }
/* 173 */     return method;
/*     */   }
/*     */   
/*     */   protected HttpMethod execute(DavRequest req) throws IOException {
/* 177 */     if (this.mDebugEnabled)
/* 178 */       ZimbraLog.dav.debug("Request payload: \n" + req.getRequestMessageString());
/* 179 */     HttpMethod m = req.getHttpMethod(this.mBaseUrl);
/* 180 */     for (Pair<String, String> header : req.getRequestHeaders()) {
/* 181 */       m.addRequestHeader((String)header.getFirst(), (String)header.getSecond());
/*     */     }
/* 183 */     return executeMethod(m, req.getDepth());
/*     */   }
/*     */   
/* 186 */   protected HttpMethod executeMethod(HttpMethod m, DavContext.Depth d) throws IOException { ZimbraLog.dav.debug("WebDAV request (depth=" + d + "): " + m.getPath());
/*     */     
/* 188 */     HttpMethodParams p = m.getParams();
/* 189 */     if (p != null) {
/* 190 */       p.setCredentialCharset("UTF-8");
/*     */     }
/* 192 */     m.setDoAuthentication(true);
/* 193 */     m.setRequestHeader("User-Agent", this.mUserAgent);
/* 194 */     String depth = "0";
/* 195 */     switch (d) {
/*     */     case one: 
/* 197 */       depth = "1";
/* 198 */       break;
/*     */     case infinity: 
/* 200 */       depth = "infinity";
/*     */     }
/*     */     
/* 203 */     m.setRequestHeader("Depth", depth);
/* 204 */     HttpClientUtil.executeMethod(this.mClient, m);
/* 205 */     if ((this.mDebugEnabled) && (m.getResponseBody() != null)) {
/* 206 */       ZimbraLog.dav.debug("WebDAV response:\n" + new String(m.getResponseBody(), "UTF-8"));
/*     */     }
/* 208 */     return m;
/*     */   }
/*     */   
/*     */   public void setCredential(String user, String pass) {
/* 212 */     this.mUsername = user;
/* 213 */     this.mPassword = pass;
/* 214 */     HttpState state = new HttpState();
/* 215 */     Credentials cred = new UsernamePasswordCredentials(this.mUsername, this.mPassword);
/* 216 */     state.setCredentials(AuthScope.ANY, cred);
/* 217 */     this.mClient.setState(state);
/* 218 */     ArrayList<String> authPrefs = new ArrayList();
/* 219 */     authPrefs.add("Basic");
/* 220 */     this.mClient.getParams().setParameter("http.auth.scheme-priority", authPrefs);
/* 221 */     this.mClient.getParams().setAuthenticationPreemptive(true);
/*     */   }
/*     */   
/*     */   public void setAuthCookie(ZAuthToken auth) {
/* 225 */     Map<String, String> cookieMap = auth.cookieMap(false);
/* 226 */     if (cookieMap != null) {
/* 227 */       String host = null;
/*     */       try {
/* 229 */         host = new URL(this.mBaseUrl).getHost();
/*     */       }
/*     */       catch (Exception e) {}
/* 232 */       HttpState state = new HttpState();
/* 233 */       for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
/* 234 */         state.addCookie(new Cookie(host, (String)ck.getKey(), (String)ck.getValue(), "/", null, false));
/*     */       }
/* 236 */       this.mClient.setState(state);
/* 237 */       this.mClient.getParams().setCookiePolicy("compatibility");
/*     */     }
/*     */   }
/*     */   
/*     */   public String getUsername() {
/* 242 */     return this.mUsername;
/*     */   }
/*     */   
/* 245 */   public String getPassword() { return this.mPassword; }
/*     */   
/*     */   public void setDebugEnabled(boolean b)
/*     */   {
/* 249 */     this.mDebugEnabled = b;
/*     */   }
/*     */   
/*     */   public void setUserAgent(String ua) {
/* 253 */     this.mUserAgent = ua;
/*     */   }
/*     */   
/*     */   public void setAppName(String app) {
/* 257 */     this.mUserAgent = ("Zimbra " + app + "/" + BuildInfo.VERSION + " (" + BuildInfo.DATE + ")");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 265 */   private boolean mDebugEnabled = false;
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/client/WebDavClient.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */